package com.compass.doctor.ui.mine.fragment

import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.databinding.FragmentBaseListBinding
import com.compass.doctor.ui.message.viewmodel.MessageViewModel
import com.compass.doctor.ui.mine.adapter.TestAndInspectionAdapter
import com.compass.doctor.base.BaseVBFragment
import com.compass.doctor.callback.OnCustomButtonClick
import com.compass.doctor.ui.mine.viewmodel.MineViewModel
import com.compass.doctor.ui.patient.InspectionReportListActivity
import com.compass.doctor.ui.patient.viewmodel.PatientViewModel
import com.compass.framework.R
import com.compass.framework.constant.KEY_ORDER_TYPE
import com.compass.framework.decoration.NormalItemDecoration
import com.compass.framework.dialog.CommonMessageDialog
import com.compass.framework.ext.gone
import com.compass.framework.ext.load
import com.compass.framework.ext.visible
import com.compass.framework.utils.dpToPx
import com.compass.framework.utils.getStringFromResource
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * Description： 检验检查Fragment
 * Date:2023/12/21 21:14
 */
class TestAndInspectionFragment : BaseVBFragment<FragmentBaseListBinding>(),
    OnLoadMoreListener,
    OnRefreshListener {

    private val patientViewModel: PatientViewModel by viewModels()
    private val mineViewModel: MineViewModel by viewModels()
    private lateinit var mAdapter: TestAndInspectionAdapter
    private var mPage = 1
    private var orderType: Int = 1//1-检验 2-检查
    override fun initView(view: View, savedInstanceState: Bundle?) {
        arguments?.let {
            orderType = it.getInt(KEY_ORDER_TYPE)
        }
        initRecyclerView()
    }

    private var onBottomClick = object : OnCustomButtonClick {
        override fun onButtonCallBack(position: Int, msgType: String?) {
            val item = mAdapter.getItem(position)
            item?.let { order ->
                when (msgType) {
                    "cancel" -> {

                        CommonMessageDialog.Builder(requireActivity())
                            .setTitle(getStringFromResource(R.string.dialog_tips_title))
                            .setMessage("是立即取消当前检验检查?")
                            .setConfirm(getStringFromResource(R.string.default_confirm))

                            .setCancel(getString(com.compass.framework.R.string.default_cancel))
                            .setonCancelListener {
                                it?.dismiss()
                            }
                            .setonConfirmListener {
                                patientViewModel.cancelInspection(order.id)
                                it?.dismiss()
                            }.create().show()

                    }


                }
            }

        }
    }

    private fun initRecyclerView() {
        mBinding?.refreshLayout?.apply {
            setEnableRefresh(true)
            setEnableLoadMore(true)
            setOnRefreshListener(this@TestAndInspectionFragment)
            setOnLoadMoreListener(this@TestAndInspectionFragment)
        }
        mAdapter = TestAndInspectionAdapter()
        val dp10 = dpToPx(10)
        mBinding?.recyclerView?.apply {
            layoutManager = LinearLayoutManager(context)
            addItemDecoration(NormalItemDecoration().apply {
                setBounds(left = dp10, top = dp10, right = dp10, bottom = dp10)
                setLastBottom(true)
            })
            adapter = mAdapter
        }
        mAdapter.setBottomCallBack(onBottomClick)
        mAdapter.onItemClickListener = { _: View, position: Int ->
            val item = mAdapter.getItem(position)
            item?.let {
                if (orderType==1){
                    InspectionReportListActivity.start(requireContext(),it.patientId,0)
                }else{
                    InspectionReportListActivity.start(requireContext(),it.patientId,1)
                }
            }
        }
    }


    override fun initData() {
        showLoading()
        getDoctorInspectionList()
        patientViewModel.cancelInspectionLiveData.observe(this){
            it?.let {
                getDoctorInspectionList()
            }
        }
        mineViewModel.inspectionRecordLiveData.observe(this){
            dismissLoading()
            if (mPage==1){
                mAdapter.setData(it)
                mBinding?.refreshLayout?.finishRefresh()
                mBinding?.refreshLayout?.setEnableLoadMore(load(it?.size))
                if (it.isNullOrEmpty()) {
                    mBinding?.emptyView?.visible()
                } else {
                    mBinding?.emptyView?.gone()
                }
            }else{
                mBinding?.refreshLayout?.finishLoadMore()
                if (!it.isNullOrEmpty()){
                    mAdapter.addAll(it)
                }else{
                    mBinding?.refreshLayout?.setEnableLoadMore(false)
                }
            }

        }
    }


    private fun getDoctorInspectionList() {
        mineViewModel.getDoctorInspectionList(mPage, 10,orderType)

    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        mPage++
        getDoctorInspectionList()
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        mPage = 1
        getDoctorInspectionList()
    }
}